@rem======================================================================
@rem
@rem    setup.bat
@rem
@rem======================================================================

@echo off
setlocal
pushd .

goto LInitialize


@rem----------------------------------------------------------------------
@rem    LInitialize
@rem----------------------------------------------------------------------
:LInitialize
    set SPAdminTool=%CommonProgramFiles%\Microsoft Shared\web server extensions\12\BIN\stsadm.exe
    set Install=
    set Uninstall=
	set UninstallInstall=
    set PackageFile=DVK.SP2007.wsp
    set PackageName=DVK.SP2007.wsp
    set DefaultWebUrl=
    set DefaultSiteUrl=
    set TargetWebUrl=
    set TargetSiteUrl=
    set FeatureGuid1=
    set FeatureGuid2=
    set FeatureGuid3=

    goto LParseArgs


@rem----------------------------------------------------------------------
@rem    LParseArgs
@rem----------------------------------------------------------------------
:LParseArgs
    @rem --- help ---
    if "%1" == "/?"    goto LHelp
    if "%1" == "/help" goto LHelp

    @rem --- Fix execute task ---
	if "%1" == "/ui"		(set UninstallInstall=1) & shift & goto LParseArgs
    if "%1" == "/fguid1"	(set FeatureGuid1=%2) & shift & shift & goto LParseArgs
    if "%1" == "/fguid2"	(set FeatureGuid2=%2) & shift & shift & goto LParseArgs
    if "%1" == "/fguid3"	(set FeatureGuid3=%2) & shift & shift & goto LParseArgs
    if "%1" == "/i"         (set Install=1)   & shift & goto LParseArgs
    if "%1" == "/u"         (set Uninstall=1) & shift & goto LParseArgs
    
    @rem --- Fix url ---
    if "%1" == "/weburl"  (set TargetWebUrl=%2)  & shift & shift & goto LParseArgs
    if "%1" == "-weburl"  (set TargetWebUrl=%2)  & shift & shift & goto LParseArgs
    if "%1" == "/siteurl" (set TargetSiteUrl=%2) & shift & shift & goto LParseArgs
    if "%1" == "-siteurl" (set TargetSiteUrl=%2) & shift & shift & goto LParseArgs

    @rem --- Check invalid arguments ---
    if not "%1" == "" (
        echo Invalid argument.
        goto LHelp
    )

    @rem --- Check arguments ---
    if "%Install%" == "1" (
        if "%Uninstall%" == "1" (
            goto LHelp
        )
    )

    if "%Install%" == "" (
        if "%Uninstall%" == "" (
			if "%UninstallInstall%" == "" (
				set Install=1
			)
        )
    )
    
	goto LMain


@rem----------------------------------------------------------------------
@rem	LHelp
@rem----------------------------------------------------------------------
:LHelp
    echo Usage:
    echo setup.bat [/install or /uninstall][/weburl ^<url^>][/siteurl ^<url^>]
    echo           [/help]
    echo.
    echo Options:
    echo  /install or /uninstall
    echo  Install specified Solution package (.wsp) to the SharePoint server
    echo  or uninstall specified Solution from the SharePoint server.
    echo  Default value: install
    echo  /weburl
    echo  Specify a web url of the SharePoint server.
    echo  Default value: %DefaultWebUrl%
    echo  /siteurl
    echo  Specify a site url of the SharePoint server.
    echo  Default value: %DefaultSiteUrl%
    echo  /help
    echo  Show this information.
    echo /ui
    echo Uninstall first and then install.
    echo.

	goto LTerminate


@rem----------------------------------------------------------------------
@rem    LMain
@rem----------------------------------------------------------------------
:LMain
	if "%Install%" == "1" (
      call :LDeploy
  )
	if "%Uninstall%" == "1" (
      call :LRetract
  )
  if "%UninstallInstall%" == "1" (
	  call :LUninstallInstall
  )

	goto LTerminate


@rem----------------------------------------------------------------------
@rem    LDeploy
@rem----------------------------------------------------------------------
:LDeploy

@rem -url %TargetWebUrl%, -url %TargetSiteUrl%

    echo Deploying solution %PackageName% ... 
    "%SPAdminTool%" -o deploysolution -name "%PackageName%" -url %TargetWebUrl% -local -allowGacDeployment 

    echo Activating feature SP.DVK.Listid ... Guid="%FeatureGuid1%"
    "%SPAdminTool%" -o activatefeature -id "%FeatureGuid1%" -url %TargetSiteUrl%

    echo Activating feature SP.DVK ... Guid="%FeatureGuid2%"
    "%SPAdminTool%" -o activatefeature -id "%FeatureGuid2%" -url %TargetSiteUrl%

    echo Activating feature SP.DVK.Jobid ... Guid="%FeatureGuid3%"
    "%SPAdminTool%" -o activatefeature -id "%FeatureGuid3%" -url %TargetSiteUrl%

	echo Staring IIS reset
    "C:\Windows\System32\IISReset.exe" /NOFORCE

    goto :EOF


@rem----------------------------------------------------------------------
@rem    LRetract
@rem----------------------------------------------------------------------
@rem  -url %TargetSiteUrl%, -url %TargetWebUrl%

:LRetract

    echo Deactivating feature SP.DVK.Jobid ... Guid="%FeatureGuid3%"
    "%SPAdminTool%" -o deactivatefeature -id "%FeatureGuid3%" -url %TargetSiteUrl%

    echo Deactivating feature SP.DVK ... Guid="%FeatureGuid2%"
    "%SPAdminTool%" -o deactivatefeature -id "%FeatureGuid2%" -url %TargetSiteUrl%

    echo Deactivating feature SP.DVK.Lists ... Guid="%FeatureGuid1%"
    "%SPAdminTool%" -o deactivatefeature -id "%FeatureGuid1%" -url %TargetSiteUrl%

    echo Retracting solution %PackageName% ...
    "%SPAdminTool%" -o retractsolution -name "%PackageName%" -url %TargetWebUrl% -local 

    
    echo Staring IIS reset
    "C:\Windows\System32\IISReset.exe" /NOFORCE

if "%UninstallInstall%" == "1" (
		goto :LDeploy
	)
    
    goto :EOF
    
@rem----------------------------------------------------------------------
@rem    LUninstallInstall
@rem----------------------------------------------------------------------
@rem  ,  -url %TargetWebUrl%, -url %TargetWebUrl%, 
:LUninstallInstall

goto :LRetract

goto :EOF


@rem----------------------------------------------------------------------
@rem    LTerminate
@rem----------------------------------------------------------------------
:LTerminate
    set UserInput=
    @rem set /P UserInput=Hit enter key to quit.

    set SPAdminTool=
    set PackageFile=
    set PackageName=
    set Install=
    set Uninstall=
    set TargetSiteUrl=
    set TargetWebUrl=
    set UserInput=
    set UninstallInstall=


popd
endlocal